const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
Page({
  data: {
    defaultAddress:'',
    goodsDetailsdata:'',
    selectitem:[],
    totalprice:0,
    total:0,
    totalnums:0,
    packFee:0,
    number:1,
    type:'',
    remark:'',
    ordertype:'',
    source:''
  },
  onLoad: function (options) {


    if(options.type){
      this.setData({
        id:options.id,
        type:options.type,
        ordertype:options.ordertype,
        source:options.source
      })
      if(options.type=='cart'){
        // this.getDefaultAddress()
        this.cartlist()
      
      }
      if(options.type=='goods'){

   
        this.goodsDetail()
        // this.getDefaultAddress()
      }
      if(options.type=='other'){
        this.contentdetail()
      }
      if(options.type=='vip'){
        this.vipPackageList()
      }
      if(options.type=='recipe'){
        this.recipedetail()
      }
      if(options.type=='course'){
        this.lessondetail()
      }
      
    }
    if(options.parentid){
      app.globalData.parentid=options.parentid
    }
  },
  onShow: function () {
    
    if(this.data.type!='vip' && this.data.type!='other' && this.data.type!='recipe' && this.data.type!='course'){
      if(this.data.defaultAddress){
        this.getDefaultAddress()
      }
    }
  },
  adress(){
    var id=this.data.defaultAddress.id || ''
    wx.navigateTo({
      url: '/pages/mall/adress?id='+ id +'&page=order'
    })
  },
  vipPackageList(source){
    Controller.vipPackageList().then(res=>{
      if(res.data.baseData && res.data.baseData.length>0){
        for(var item of res.data.baseData){
          consoelo.log('==')
          if(item.id==this.data.id){
            item.amount=1
            item.specs=""
            item.goodsId=item.id
            var cartlistdata=[]
            cartlistdata.push(item)
            this.setData({
              cartlistdata
            })
            this.totalnums()
          }
        }
      }
    })
  },
  freight(){
   
    
  let list=  this.data.cartlistdata
  let goodsIds=[]
  let goodsAmounts=[] 

  list.map((item,index)=>{
     goodsIds.push(item.goodsId)
     goodsAmounts.push(item.amount)
  })
  
 if(goodsIds.length>1&&goodsAmounts.length>1){
  goodsIds=goodsIds.join(',')
  goodsAmounts=goodsAmounts.join(',')
 }else{
    goodsIds=goodsIds[0]
    goodsAmounts=goodsAmounts[0]
 }


  

  
  
    Controller.freight({province:this.data.defaultAddress.province,goodsIds,goodsAmounts}).then(res=>{
     
        this.setData({
          packFee:res.data.baseData
        })
     
     
      this.totalnums()
    })
  },
  getDefaultAddress(){
   
    Controller.getDefaultAddress().then(res=>{
      if(res.data.baseData){
        this.setData({
          defaultAddress:res.data.baseData
        })
        this.freight()
      }
    })
  },
  cartlist(){
    Controller.cartlist().then(res=>{
      if(res.data.baseData){
        var cartids=this.data.id.split(",");
        var lists=[]
        for(var item of res.data.baseData){
          for(var item2 of cartids){
            if(item2==item.cartId){
              lists.push(item)
            }
          }
        }
        this.setData({
          cartlistdata:lists
        },()=>{
           this.getDefaultAddress()
        })
      
        this.totalnums()
      }
    })
  },
  recipedetail(){
    Controller.recipedetail({recipeId:this.data.id}).then(res=>{
      if(res.data.baseData){
        res.data.baseData.imgUrl=res.data.baseData.iconImgUrl
        res.data.baseData.amount=1
        res.data.baseData.specs=""
        res.data.baseData.goodsId=res.data.baseData.id
        res.data.baseData.discountPrice=res.data.baseData.price
        var cartlistdata=[]
        cartlistdata.push(res.data.baseData)
      
        this.setData({
          cartlistdata
        })
        this.totalnums()
      }
    })
  },
  lessondetail(){
    Controller.lessondetail({lessonId:this.data.id}).then(res=>{
      if(res.data.baseData){
        res.data.baseData.imgUrl=res.data.baseData.imgUrl
        res.data.baseData.amount=1
        res.data.baseData.specs=""
        res.data.baseData.goodsId=res.data.baseData.id
        res.data.baseData.discountPrice=res.data.baseData.price
        var cartlistdata=[]
        cartlistdata.push(res.data.baseData)
        this.setData({
          cartlistdata
        })
        this.totalnums()
      }
    })
  },
  contentdetail(){
    Controller.contentdetail({contentId:this.data.id}).then(res=>{
      if(res.data.baseData){
        res.data.baseData.imgUrl=res.data.baseData.iconImgUrl
        res.data.baseData.amount=1
        res.data.baseData.specs=""
        res.data.baseData.goodsId=res.data.baseData.id
        res.data.baseData.discountPrice=res.data.baseData.price
        var cartlistdata=[]
        cartlistdata.push(res.data.baseData)
        this.setData({
          cartlistdata
        })
        this.totalnums()
      }
    })
  },
  goodsDetail(){
    Controller.goodsdetail({goodsId:this.data.id}).then(res=>{
      if(res.data.baseData){
        res.data.baseData.amount=1
        res.data.baseData.goodsId=res.data.baseData.id
        var cartlistdata=[]
        cartlistdata.push(res.data.baseData)
        this.setData({
          cartlistdata
        },()=>{
          this.getDefaultAddress()
        })
        // this.totalnums()
      }
    })
  },
  mins(e){
    var id=e.currentTarget.dataset.id;
    var lists=this.data.cartlistdata;
    var selectitem=""
    for(var item of lists){
      if(item.goodsId==id){
        item.amount--;
        if(item.amount<=0){
          item.amount=1
          return
        }
        selectitem=item
      }
    }
    if(this.data.type=='cart'){
      this.addOrUpdate(selectitem,1,2,lists)
    }else{
      this.setData({
        cartlistdata:lists
      },()=>{
        this.freight()
      })
      this.totalnums()
    }
  },
  plus(e){
    var id=e.currentTarget.dataset.id;
    var lists=this.data.cartlistdata;
    var selectitem=""
    for(var item of lists){
      if(item.goodsId==id){
        item.amount++;
        if(item.amount>=item.stock){
          item.amount=item.stock
          wx.showToast({
            title:"库存不足",
            icon:"none"
          })
          return
        }
        selectitem=item
      }
    }
    if(this.data.type=='cart'){
      this.addOrUpdate(selectitem,1,1,lists)
    }else{
      this.setData({
        cartlistdata:lists
      },()=>{
        this.freight()
      })
      this.totalnums()
    }
  },
  changenums(e){
    var id=e.currentTarget.dataset.id;
    var lists=this.data.cartlistdata;
    var selectitem=""
    var oldnum=0
    var type=1
    var disnums=0
    for(var item of lists){
      if(item.goodsId==id){
        oldnum=item.amount
        item.amount=e.detail.value;
        if(item.amount<=0){
          item.amount=1
          return
        }
        if(item.amount>=item.stock){
          item.amount=item.stock
          wx.showToast({
            title:"库存不足",
            icon:"none"
          })
          this.setData({
            cartlistdata:lists
          })
          return
        }
        selectitem=item
        if(oldnum>item.amount){
          type=2
          disnums=oldnum-item.amount
        }
        if(oldnum<item.amount){
          type=1
          disnums=item.amount-oldnum
        }
      }
    }
    
    if(this.data.type=='cart'){
      this.addOrUpdate(selectitem,disnums,type,lists)
    }else{
      this.setData({
        cartlistdata:lists
      })
      // this.totalnums()
    }
  },
  addOrUpdate(selectitem,num,type,lists){
    var data={
      goodsId:selectitem.goodsId,
      specs:selectitem.specs,
      amount:num,
      type:type
    }
    Controller.addOrUpdate(data).then(res=>{
      this.setData({
        cartlistdata:lists
      },()=>{
         this.getDefaultAddress()
        // this.freight()
      })
      // this.totalnums()
    })
  },
  totalnums(){
    var lists=this.data.cartlistdata;
   

    var number=0;
    var price=0;
    var selectitem=[]
    for(var item of lists){
      selectitem.push(item)
      number+=Number(item.amount);
      price+=Number(item.amount*item.discountPrice)
    }
    this.setData({
      totalprice:price.toFixed(2),
      totalnums:number,
      total:(price+Number(this.data.packFee)).toFixed(2),
      selectitem
    })
  },
  getremark(e){
    this.setData({
      remark:e.detail.value
    })
  },
  //支付
  pay() {
    if(app.globalData.isios){
      wx.showToast({
        title:"ios提示不能支付",
        icon:"none"
      })
      return
    }
    if(this.data.type=='cart'){
      if(!this.data.defaultAddress.id){
        wx.showToast({
          title: "请添加收货地址",
          icon: 'none',
          duration: 1000
        });
        return
      }
      Controller.ordercreateCart({cartIds:this.data.id,addressId:(this.data.defaultAddress.id || ''),remark:this.data.remark}).then(res=>{
        wx.redirectTo({
          url: '/pages/mall/pay?id='+ res.data.baseData +'&type='+this.data.type +'&price='+this.data.total
        })
      })
    }
    if(this.data.type=='goods'){
      if(!this.data.defaultAddress.id){
        wx.showToast({
          title: "请添加收货地址",
          icon: 'none',
          duration: 1000
        });
        return
      }
      var goods=this.data.cartlistdata[0];
      Controller.ordercreate({goodsId:goods.id,amount:goods.amount,addressId:(this.data.defaultAddress.id || ''),remark:this.data.remark}).then(res=>{
        wx.redirectTo({
          url: '/pages/mall/pay?id='+ res.data.baseData +'&type='+this.data.type +'&price='+this.data.total
        })
      })
    }
    if(this.data.type=='other' || this.data.type=='vip' || this.data.type=='recipe' || this.data.type=='course'){
      // 1是资讯，2是养生课程，3.养生视频，5是食谱，6vip
      var goods=this.data.cartlistdata[0];
      Controller.ordercreateItem({itemId:goods.id,addressId:(this.data.defaultAddress.id || ''),orderType:this.data.ordertype}).then(res=>{
        wx.redirectTo({
          url: '/pages/mall/pay?id='+ res.data.baseData +'&type='+this.data.type +'&price='+this.data.total
        })
      })
    }
    
  },
  
})